package com.alan.android.sqlite;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import com.alan.android.sqlite.table.Table;
import com.alan.android.sqlite.table.TableFactory;

import java.util.List;

public class AndroidInnerSqlite extends SQLiteOpenHelper {

    private List<Class<?>> modelList;

    private static final String TAG = "AndroidInnerSqlite";

    public AndroidInnerSqlite(Context context, String name, int version, List<Class<?>> modelList) {
        super(context, name, null, version);
        this.modelList = modelList;
    }

    public AndroidInnerSqlite(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        Log.d(TAG, "onCreate()");
        for (Class<?> model : modelList) {
            try {
                Table table = TableFactory.getTable(model);
                String sql = table.getCreateSqlStr();
                Log.d(TAG, "onCreate sql:" + sql + " table name:" + table.getTableName());
                db.execSQL(sql);
            } catch (Exception e) {
                Log.e(TAG, Log.getStackTraceString(e));
            }
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}
